package com.lionxing.DNTG;

import org.cocos2d.actions.CCProgressTimer;
import org.cocos2d.events.CCTouchDispatcher;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.protocols.CCTouchDelegateProtocol;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;

import android.view.MotionEvent;

public class BarSpirte extends CCSprite implements CCTouchDelegateProtocol
{

	
    CGPoint _beginPoint;
    
	int _type; // 0bg 1effect
	public static BarSpirte sprite(String filename)
	{
		return new BarSpirte(filename);
		
	}
	
	public BarSpirte(String filename)
	{
		super(filename);
		
		CCProgressTimer left = CCProgressTimer.progress("option_arr.png");
		left.setType(CCProgressTimer.kCCProgressTimerTypeHorizontalBarLR);
		addChild(left, 10, 20);
		left.setPosition(CGPoint.make(147, 28));
	}
	
	 public void onEnter()
	{

		CCTouchDispatcher.sharedDispatcher().addTargetedDelegate(this, 0,true);
		super.onEnter();
	}

	 
	 public  void onExit()
	{		
		CCTouchDispatcher.sharedDispatcher().removeDelegate(this);
		super.onExit();
	}
	
	 public boolean ccTouchesBegan(MotionEvent event)
	    {
		 
		 if(!containsTouchLocation(event)) return false;
		 
		 _beginPoint =  CCDirector.sharedDirector()
     	.convertToGL(CGPoint.make(event.getX(), event.getY()));
	    	return true;
	    	
	    }

	    public boolean ccTouchesMoved(MotionEvent event)
	    {
	    	CGPoint convertedLocation = CCDirector.sharedDirector()
        	.convertToGL(CGPoint.make(event.getX(), event.getY()));
	    	CCProgressTimer pro = (CCProgressTimer)this.getChildByTag(20);
	    	
	    	if(_beginPoint.x > convertedLocation.x)
	    	{
	    		
	    		if (pro.getPercentage() <= 0.0f)
	    		{
	    			
	    			return true;
	    		}
	    		
	    		pro.setPercentage((pro.getPercentage() - (_beginPoint.x - convertedLocation.x)/2));
	    		_beginPoint = convertedLocation;
	    		if(pro.getPercentage() <= 0.0f)
	    		{
	    			pro.setPercentage(0.0f);
	    		}
	    	}
	    	else if(_beginPoint.x < convertedLocation.x) 
	    	{
	    		
	    		if(pro.getPercentage() >= 100.0f)
	    		{
	    			return true;
	    			
	    		}
	    		
	    		pro.setPercentage((pro.getPercentage() + ( convertedLocation.x - _beginPoint.x)/2));
	    		_beginPoint = convertedLocation;
	    		if(pro.getPercentage() >= 100.0f)
	    		{
	    			pro.setPercentage(100.0f);
	    			
	    		}
	    		
	    	}
	    	
	    	if(_type == 0)
	    	{
	    		float tmp = pro.getPercentage()/100.0f;
	    		
	    		SoundEngine.sharedEngine().setSoundVolume(tmp);
	    	}
	    	else if(_type == 1)
	    	{
	    		float tmp = pro.getPercentage()/100.0f;
	    		
	    		SoundEngine.sharedEngine().setEffectsVolume(tmp);
	    	}
	    	else
	    	{
	    		
	    	}
	    	
	    	return true;
	    }

	    public boolean ccTouchesEnded(MotionEvent event)
	    {
	    	
	    	return true;
	    }

	    public boolean ccTouchesCancelled(MotionEvent event)
	    {
	    	
	    	return true;
	    }
	    
	    
	    private boolean containsTouchLocation(MotionEvent event)
	    {
	    	
	    	CGRect rect = rect();
	    	
	    	return CGRect.containsPoint(rect(), convertTouchToNodeSpaceAR(event));
	    	
	    }
	    
	    
	    private CGRect rect()
	    {
	    	// NSLog([self description]);
	    	//return CGRectMake(-rect_.size.width / 2, -rect_.size.height / 2, rect_.size.width, rect_.size.height);
	    	
	    	return CGRect.make(-getTextureRect().size.width/2, -getTextureRect().size.height/2, getTextureRect().size.width, getTextureRect().size.height);
	    }
	    
	    
	    public float getPercent()
	    {
	    	CCProgressTimer pro = (CCProgressTimer)this.getChildByTag(20);
	    	
	    	return pro.getPercentage();
	    }
	    
	    public void setPercent(float per)
	    {
	    	
	    	CCProgressTimer pro = (CCProgressTimer)this.getChildByTag(20);
	    	
	    	pro.setPercentage(per);
	    }
	    
	    public void setSptype(int type)
	    {
	    	
	    	_type = type;
	    	
	    }
}
